<template>
  <div class="register">
    <h2>注册</h2>
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit" @click="register">
          注册
        </van-button>
      </div>
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit" @click="login">
          已有账号点击登录
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
export default {
  name: "register",
  data() {
    return {
      username: "",
      password: "",
    };
  },
  methods: {
    onSubmit(values) {
      console.log("submit", values);
    },
    register() {
      if (this.username && this.password) {
        window.sessionStorage.setItem("user", this.username);
      } else {
        alert("账号或密码不能为空");
      }
      alert('注册成功')
      this.$router.push('/login')
    },
    login(){
      this.$router.push('/login')
    }
  },
};
</script>

<style scoped>
.login {
  padding: 0 20px;
}
h2 {
  line-height: 50px;
  text-align: center;
}
</style>
